/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package ca.trader.comment;

import ca.trader.db.CommentTableConstraints;
import ca.trader.sale.Sale;
import ca.trader.user.User;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.PrePersist;
import javax.persistence.SequenceGenerator;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author Ahsan
 */
@Entity
@NamedQueries(
{
	 // Query for finding comment by sale.
	 @NamedQuery(name = CommentEntityConsts.NAMED_QUERY_FIND_COMMENTS_BY_SALE,
	 query = "SELECT c FROM Comment c WHERE c.sale.id = :" + CommentEntityConsts.NAMED_QUERY_FIND_COMMENTS_BY_SALE_PARAM_SALE_ID + " ORDER BY c.id")
})
@XmlRootElement
public class Comment implements Serializable
{

	 /**
	  * Stores the ID of the comment. This value is auto-generated and should
	  * not be set explicitly.
	  *
	  * This value is merely meant to satisfy schema requirements and has no
	  * logical meaning. The ID of a comment cannot be associated to the sale
	  * it's in, the user who created the sale and son.
	  */
	 @Id
	 @SequenceGenerator(name = "COMMENT_D_SEQ", allocationSize = 1, initialValue = 1)
	 @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "COMMENT_D_SEQ")
	 @Column(length = CommentTableConstraints.CONSTRAINT_COMMENT_ID_MAX_LENGTH, nullable = false)
	 private Integer id;

	 /**
	  * Stores the user object representing the user who made the comment
	  */
	 @ManyToOne ( cascade ={
		  CascadeType.MERGE, CascadeType.REFRESH
	 }, fetch = FetchType.LAZY)
	 @JoinColumn(nullable = false)	 
	 private User commentor;

	 /**
	  * Stores the sale object representing the sale where this comment was
	  * made
	  */
	 @ManyToOne ( cascade ={
		  CascadeType.MERGE, CascadeType.REFRESH
	 }, fetch = FetchType.LAZY)
	 @JoinColumn(nullable = false)
	 private Sale sale;

	 /**
	  * Stores the time the comment was made. This value is auto-generated and
	  * should not be set explicitly.
	  */
	 @Temporal(TemporalType.TIMESTAMP)
	 @Column(nullable = false)
	 private Date commentDate;

	 /**
	  * Stores the actual comment that was made as a string.
	  */
	 @Column(length = CommentTableConstraints.CONSTRAINT_COMMENT_MAX_LENGTH, nullable = false)
	 private String commentContent;

	 /**
	  * @return the id
	  */
	 public Integer getId()
	 {
		  return id;
	 }

	 /**
	  * @param id the id to set
	  */
	 public void setId(Integer id)
	 {
		  this.id = id;
	 }

	 /**
	  * @return the commentor
	  */
	 public User getCommentor()
	 {
		  return commentor;
	 }

	 /**
	  * @param commentor the commentor to set
	  */
	 public void setCommentor(User commentor)
	 {
		  this.commentor = commentor;
	 }

	 /**
	  * @return the sale
	  */
	 public Sale getSale()
	 {
		  return sale;
	 }

	 /**
	  * @param sale the sale to set
	  */
	 public void setSale(Sale sale)
	 {
		  this.sale = sale;
	 }

	 /**
	  * @return the commentDate
	  */
	 public Date getCommentDate()
	 {
		  return commentDate;
	 }

	 /**
	  * @return the commentContent
	  */
	 public String getCommentContent()
	 {
		  return commentContent;
	 }

	 /**
	  * @param commentContent the commentContent to set
	  */
	 public void setCommentContent(String commentContent)
	 {
		  this.commentContent = commentContent;
	 }

	 /**
	  * Private method to set the created date of the comment. Called
	  * automatically (no explicit call)
	  */
	 @PrePersist
	 private void updateCommentDate()
	 {
		  commentDate = new Date();
	 }

	 
	 @Override
	 public boolean equals(Object o)
	 {
		  if (!(o instanceof Comment))
		  {
			   return false;
		  }

		  Comment sale = (Comment) o;

		  return this.getId().equals(sale.getId());
	 }

}
